package com.orhon.smartcampus.modules.base.fetcher;

import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SchoolFetcher implements DataFetcher {
	@Autowired
	SchoolsDao schoolsDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Schools where deleted_at = null and id = "+id;
		Schools item = schoolsDao.findId(hql, null);
		if(item!=null) {
			
			GQLSchool parGQLSchool = null;
			if (env.getSelectionSet().contains("parent")){
				Schools schools2 = item.getSchools();
				parGQLSchool = SchoolMapper.MAPPER.toSchool(schools2);
			}
			
			List<GQLModule> gqlModule = null;
			if (env.getSelectionSet().contains("modules")){
				List<Modules> modules = item.getModules();
				 gqlModule = ModuleMapper.MAPPER.mapModuleList(modules);
			}
			
			List<GQLUser> gqlUser = null;
			if (env.getSelectionSet().contains("users")){
				List<Users> users = item.getUsers();
				gqlUser = UserMapper.MAPPER.mapList(users);
			}
			
			List<GQLStudent> gqlStudent = null;
			if (env.getSelectionSet().contains("students")){
				List<SInformation> list = item.getStudents();
				gqlStudent = StudentMapper.MAPPER.toStudentList(list);
			}
			
			List<GQLTeacher> gqlTeacher = null;
			if (env.getSelectionSet().contains("teachers")){
				List<TInformation> teachers = item.getTeachers();
				gqlTeacher = TeacherMapper.MAPPER.toTeacherList(teachers);
			}
//			SchooltypesDeleateable schoolType = null;
//			if (env.getSelectionSet().contains("teachers")){
//				List<TInformation> teachers = item.getTeachers();
//				gqlTeacher = TeacherMapper.MAPPER.toTeacherList(teachers);
//			}
			GQLSchool school = SchoolMapper.MAPPER.toSchool(item);
			school.setParent(parGQLSchool);
			school.setModules(gqlModule);
			school.setUsers(gqlUser);
			school.setStudents(gqlStudent);
			school.setTeachers(gqlTeacher);
			return school;
		}
		return null;
		
	}


}
